package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getMchRiskConfigBean;
import com.ymatou.iapi.godmch.parameter.setShareTypeBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getMchRiskConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_config;
import com.ymttest.database.model.godmch.mch_configExample;
import com.ymttest.database.sqlwapper.godmch.mch_configWapper;
import com.ymttest.utils.EnvSetup;
/**
*获取商户风控配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getMchRiskConfig{
	private static getMchRiskConfigBean bean;
	private static getMchRiskConfigCall call;
	private mch_configWapper mchconfigWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户风控配置");
	}

	@Before
	public void caseUp() {
		bean=new getMchRiskConfigBean();
		call=new getMchRiskConfigCall();
		mchconfigWapper=new mch_configWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verfiy(boolean isDefault,setShareTypeBean... setShareTypeBean) throws InterruptedException
	{
		Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
        //风控字段
        mch_configExample example=new mch_configExample();
        example.createCriteria().andMchIdEqualTo(bean.getMchId());
        mch_config mchconfig=mchconfigWapper.selectByExample(example).get(0);
        //默认风控值
        String exp_productRiskAmount="";
        String exp_tradeRiskAmount="";
        JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
        if(isDefault==true)
        {
           exp_productRiskAmount=EnvSetup.getData("productRiskAmount");
           exp_tradeRiskAmount=EnvSetup.getData("tradeRiskAmount");
        }
        else
        {
           if(setShareTypeBean.length>0){
           exp_productRiskAmount=setShareTypeBean[0].getProductRiskAmount().stripTrailingZeros().toPlainString();
           exp_tradeRiskAmount=setShareTypeBean[0].getTradeRiskAmount().stripTrailingZeros().toPlainString();
           }
           else {
        	   Logger.verifyIsNull(false, "验证风控有问题");
		   }
        }

         Logger.verifyEquals(mchconfig.getProductRiskAmount().stripTrailingZeros().toPlainString(), exp_productRiskAmount, "验证数据库mch_confog 中字段ProductRiskAmount与默认是否一致");
         Logger.verifyEquals(mchconfig.getTradeRiskAmount().stripTrailingZeros().toPlainString(), exp_tradeRiskAmount, "验证数据库mch_confog 中字段TradeRiskAmount与默认是否一致");
          
         Logger.verifyEquals(jsonObject.getAsJsonObject().get("productRiskAmount").getAsBigDecimal().stripTrailingZeros().toPlainString(), exp_productRiskAmount, "验证返回ProductRiskAmount是否与默认的ProductRiskAmount是否一致");
         Logger.verifyEquals(jsonObject.getAsJsonObject().get("tradeRiskAmount").getAsBigDecimal().stripTrailingZeros().toPlainString(), exp_tradeRiskAmount, "验证返回ProductRiskAmount是否与默认的tradeRiskAmount是否一致");

         Logger.verifyEquals(mchconfig.getSupportHpCNY(),jsonObject.getAsJsonObject().get("supportHpCNY").getAsBoolean(), "验证数据库mch_confog 中字段supportHpCNY与默认是否一致");
         
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_001() {
		Logger.start(true,"正例-获取商户风控配置-输入新增非独立纯渠道-返回默认风控");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);

			bean.setMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			check_verfiy(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_001_1() {
		Logger.start(true,"正例-获取商户风控配置-输入新增非独立纯渠道-设置风控-返回新设置的风控");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);

			setShareTypeBean setShareTypeBean=GodmchManageCallService.initsetShareTypeV2(userId_channl);
			GodmchManageCallService.setShareTypeV2Call(setShareTypeBean);
			bean.setMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			check_verfiy(false,setShareTypeBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_002() {
		Logger.start(true,"正例-获取商户风控配置-输入新增非独立纯渠道+代理");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//创建代理
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("USA");
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			check_verfiy(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_003() {
		Logger.start(true,"正例-获取商户风控配置-输入独立商户");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			bean.setMchId(createIndepMchCall.getmchId());
			call.setData(bean);
			call.callService();			
			
	        JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
			Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段ProductRiskAmount=0,不受限制");
	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段TradeRiskAmount=0,不受限制");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_004() {
		Logger.start(true,"正例-获取商户风控配置-输入渠道分享方式是独立小程序");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);

			setShareTypeBean setShareTypeBean=GodmchManageCallService.initsetShareTypeV2(userId_channl);
			setShareTypeBean.setMchShareType(3);//分享方式1：小程序 2：H5 3：独立小程序  4：独立h5  5：继承版小程序
			setShareTypeBean.setPayMode(5);
			GodmchManageCallService.setShareTypeV2Call(setShareTypeBean);
			bean.setMchId(userId_channl);
			call.setData(bean);
			call.callService();			
		
			JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
			Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段ProductRiskAmount=0,不受限制");
	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段TradeRiskAmount=0,不受限制");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_005() {
		Logger.start(true,"正例-获取商户风控配置-输入非独立渠道是老数据(mch_config无数据)");
		try {
			
			bean.setMchId(500000004);
			call.setData(bean);
			call.callService();		
			
			JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
			Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
	        //功能上线时，所有渠道商老用户 全部默认规则二目前设置是20000 25000
	        Logger.verifyEquals("20000",new BigDecimal(jsonObject.getAsJsonObject().get("productRiskAmount").toString()).stripTrailingZeros().toPlainString(), "验证数据库mch_confog 中字段ProductRiskAmount=20000");
	        Logger.verifyEquals("25000",new BigDecimal(jsonObject.getAsJsonObject().get("tradeRiskAmount").toString()).stripTrailingZeros().toPlainString(), "验证数据库mch_confog 中字段tradeRiskAmount=25000");
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_006() {
		Logger.start(true,"正例-获取商户风控配置-输入非独立代理是老数据(mch_config无数据)");
		try {
			bean.setMchId(500000005);
			call.setData(bean);
			call.callService();			
			//功能上线时，所有纯代理不受限制
		//	JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
			Logger.verifyEquals("5000", call.getStatus1(), "验证返回成功status值");
//	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
//	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段ProductRiskAmount=0,不受限制");
//	        Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段TradeRiskAmount=0,不受限制");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_007() {
		Logger.start(true,"正例-获取商户风控配置-输入商户Id是代理");
		try {

			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//创建代理
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			//纯代理不受风控限制
			JsonObject jsonObject=call.getJsonObject().get("result").getAsJsonObject();
			Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
		    Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
		    Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段ProductRiskAmount=0,不受限制");
		    Logger.verifyEquals("0",jsonObject.getAsJsonObject().get("productRiskAmount").toString(), "验证数据库mch_confog 中字段TradeRiskAmount=0,不受限制");
		    Logger.verifyEquals("true",jsonObject.getAsJsonObject().get("supportHpCNY").toString(), "验证数据库mch_confog 中字段supportHpCNY=true,不支持hp人民币计价");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_008() {
		Logger.start(true,"正例-获取商户风控配置-输入商户Id不存在");
		try {
			bean.setMchId(9999999);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("5000", call.getStatus1(), "验证返回成功status值");
		    Logger.verifyEquals(true, call.getMessage().contains("商户信息不存在"), "验证返回成功Message值");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_009() {
		Logger.start(true,"正例-获取商户风控配置-输入商户Id为空");
		try {
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("4001", call.getStatus1(), "验证返回成功status值");
		    Logger.verifyEquals(true, call.getMessage().contains("商户id不能为空"), "验证返回成功Message值");					
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getMchRiskConfig_010() {
		Logger.start(true,"正例-获取商户风控配置-验证mch_config=0false");
		try {
//			//渠道
//			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();//初始化创建用户 为什么手机号没有值
//			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
//			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
//			GodmchManageCallService.createmchCallForChannel(createmchbean);
			int userId_channl=500956986;
			bean.setMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			check_verfiy(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}